package com.chenyue.cm.msg.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chenyue.cm.live.dao.JdLiveMapper;
import com.chenyue.cm.live.domain.JdLive;
import com.chenyue.cm.live.domain.JdLiveRecord;
import com.chenyue.cm.match.dao.JdMatchAppointmentMapper;
import com.chenyue.cm.match.domain.JdMatchAppointment;
import com.chenyue.cm.match.domain.JdMatchFormatMember;
import com.chenyue.cm.match.domain.JdMatchWhiteApply;
import com.chenyue.cm.msg.dao.JdMsgMapper;
import com.chenyue.cm.msg.domain.JdMsg;
import com.chenyue.cm.msg.service.PushService;
import com.chenyue.cm.push.jpush.PushUtils;
import com.chenyue.cm.push.jpush.vo.NotifyType;
import com.chenyue.cm.push.jpush.vo.PushContent;
import com.chenyue.cm.push.jpush.vo.PushParam;
import com.chenyue.cm.user.dao.JdUserRelationMapper;
import com.chenyue.cm.user.domain.JdUserRelation;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

/**
 * @author libingxing
 */
@Service
public class PushServiceImpl implements PushService {

    private final JdMsgMapper msgMapper;

    private final JdUserRelationMapper userRelationMapper;

    private final JdMatchAppointmentMapper matchAppointmentMapper;

    private final JdLiveMapper liveMapper;

    public PushServiceImpl(
            @Qualifier("jdMsgMapper") JdMsgMapper msgMapper,
            @Qualifier("jdUserRelationMapper") JdUserRelationMapper userRelationMapper,
            @Qualifier("jdMatchAppointmentMapper") JdMatchAppointmentMapper matchAppointmentMapper,
            @Qualifier("jdLiveMapper") JdLiveMapper liveMapper) {
        this.msgMapper = msgMapper;
        this.userRelationMapper = userRelationMapper;
        this.matchAppointmentMapper = matchAppointmentMapper;
        this.liveMapper = liveMapper;
    }

    /**
     * 开播推送
     *
     * @param record 直播房间
     */
    @Override
    public void liveStartPush(JdLiveRecord record) {
        final JdLive live = liveMapper.selectById(record.getLiveId());

        final List<JdUserRelation> fansList = userRelationMapper.selectList(
                new LambdaQueryWrapper<JdUserRelation>()
                        .eq(JdUserRelation::getRelationUser, live.getUserId())
        );

        final List<JdMatchAppointment> appointments = matchAppointmentMapper.selectList(
                new LambdaQueryWrapper<JdMatchAppointment>()
                        .eq(JdMatchAppointment::getLiveRecordId, record.getId())
        );

        final JSONObject param = new JSONObject();
        param.put("recordId", record.getId());
        param.put("role", live.getRole());

        JdMsg msg = new JdMsg();
        msg.setTransParam(param.toJSONString());
        msg.setCt(Calendar.getInstance().getTime());

        for (JdUserRelation relation : fansList) {
            msg.setUserId(relation.getSourceUser());
            msg.setType(2);
            msg.setTransType(NotifyType.ANCHOR_RELATION.getCode());
            msg.setContent(NotifyType.ANCHOR_RELATION.getMsg());
            msgMapper.insert(msg);
            PushContent content = new PushContent();
            content.setParam(msg.getTransParam());
            content.setContent(NotifyType.ANCHOR_RELATION.getMsg());
            PushParam pushParam = new PushParam(msg.getUserId(), content, NotifyType.ANCHOR_RELATION.getCode());
            PushUtils.push(pushParam);
        }

        for (JdMatchAppointment appointment : appointments) {
            msg.setUserId(appointment.getUserId());
            msg.setType(2);
            msg.setTransType(NotifyType.LIVE_APPOINTMENT.getCode());
            msg.setContent(NotifyType.LIVE_APPOINTMENT.getMsg());
            msgMapper.insert(msg);
            PushContent content = new PushContent();
            content.setParam(msg.getTransParam());
            content.setContent(NotifyType.ANCHOR_RELATION.getMsg());
            PushParam pushParam = new PushParam(msg.getUserId(), content, NotifyType.ANCHOR_RELATION.getCode());
            PushUtils.push(pushParam);
        }
    }

    /**
     * 晋级推送
     *
     * @param formatMember 赛程记录
     */
    @Override
    public void enterFormatPush(JdMatchFormatMember formatMember) {
        PushContent content = new PushContent();
        content.setContent(NotifyType.PROMOTION_NOTIFY.getMsg());
        JSONObject json = new JSONObject();
        json.put("formatId", formatMember.getFormatId());
        final String jsonParam = json.toJSONString();
        content.setParam(jsonParam);
        PushParam param = new PushParam(formatMember.getUserId(), content, NotifyType.PROMOTION_NOTIFY.getCode());
        PushUtils.push(param);
    }

    /**
     * 白名单推送
     *
     * @param white 白名单管理记录
     */
    @Override
    public void whiteApplyPush(JdMatchWhiteApply white) {
        PushContent content = new PushContent();
        content.setContent(NotifyType.WHITE_APPLY_NOTIFY.getMsg());
        JSONObject json = new JSONObject();
        json.put("matchId", white.getMatchId());
        final String jsonParam = json.toJSONString();
        content.setParam(jsonParam);
        PushParam param = new PushParam(white.getUserId(), content, NotifyType.WHITE_APPLY_NOTIFY.getCode());
        PushUtils.push(param);
    }
}
